package logic

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"shenzhou/C/srv/internal/svc"
	"shenzhou/C/srv/model/mysql"
	"shenzhou/C/srv/pkg"
	"shenzhou/C/srv/shenzhou"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateOrderLogic) CreateOrder(in *shenzhou.CreateOrderRequest) (*shenzhou.CreateOrderResponse, error) {
	// todo: add your logic here and delete this line

	orderSn := uuid.NewString()
	o := mysql.Order{
		OrderSn: orderSn,
		GoodsId: int(in.GoodsId),
		UserId:  int(in.UserId),
		Amount:  float64(in.Amount),
		PayType: 1,
		Status:  1,
	}

	err := o.Create()
	if err != nil {
		logx.Error("订单创建失败")
		return nil, fmt.Errorf("订单创建失败")
	}
	pay := pkg.Pay(orderSn)

	return &shenzhou.CreateOrderResponse{OrderId: int64(o.ID), PayUrl: pay}, nil
}
